// dog.js
const EventEmitter = require('events');

function Dog(name, energy) {
    EventEmitter.call(this); // 继承 EventEmitter 的属性和方法
    this.name = name;
    this.energy = energy;

    const intervalId = setInterval(() => {
        if (this.energy > 0) {
            this.emit('bark');
            this.energy--;
        } else {
            clearInterval(intervalId);
        }
    }, 1000);
}

// 继承 EventEmitter 的原型
Dog.prototype = Object.create(EventEmitter.prototype);
Dog.prototype.constructor = Dog;

module.exports = Dog;